$(function(){
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组
    var root_url = $("#root_url").val();

    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent(){
        //根据条件查询订单信息
        $('.inner-section #search_btn').click(function() {
            render(true,1,pageSize);
            return false;
        });

        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function(event){
            event = event ? event: window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
                return false;
            }
        });

        //导出
        $('.row #import_btn').click(function() {
            var selectInfo = getSelectInfo();
            var data = $.param(selectInfo);
            window.location.href = "importExcel?"+data;
        });
    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function(i, field){
            selectInfo[field.name]=field.value;
        });
        JSON.stringify(selectInfo);
        return selectInfo;
    }
    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);
    }
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingWithdrawalAccount',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html+='<tr>' +
                        '<th class="th1">序号</th>' +
                        '<th class="">会员</th>' +
                        '<th class="">类型</th>' +
                        '<th class="">姓名</th>' +
                        '<th class="">账号</th>' +
                        '<th class="">银行</th>' +
                        '<th class="">添加时间</th>' +
                        // '<th class="">操作</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < myList.length;i++){
                        var obj = myList[i];
                        var num = (pageIndex-1)*pageSize + i+1;
                        var agent_id = obj.agent_id;
                        var memberUsername = obj.memberUsername;
                        var username = obj.username;
                        var nick_name = obj.nick_name;
                        var type = obj.type;
                        var account = obj.account;
                        var bankname = obj.bankname||'--';
                        var add_time = obj.add_time;
                        var id = obj.id;

                        var type_text = '微信';
                        if (type == 0) type_text = '银行卡';
                        if (type == 2) type_text = '支付宝';

                        var checked = (idList.indexOf(id) >= 0) ? "checked":"";//判断当前记录先前有没有被选中
                        if (add_time == null) add_time = '--'

                        html+='<tr>'
                            +'<td class="">'+num+'</td>'
                            +'<td class="">'+(memberUsername == null ? nick_name : memberUsername)+'</td>'
                            +'<td class="">'+type_text+'</td>'
                            +'<td class="">'+username+'</td>'
                            +'<td class="">'+account+'</td>'
                            +'<td class="">'+bankname+'</td>'
                            +'<td class="">'+add_time+'</td>'
                            +'</tr>';
                    }

                    if(myList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    }else{
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList,".inner-section #list-table .select-all",".inner-section #list-table .select-single");
                    }
                }else{
                    responseTip(result.errorCode,result.errorInfo,1500);
                }
            },
            error:errorResponse
        });
    }

    function changeStatus(){
        var id = $(this).attr("data-id");
        layer.confirm('请选择审核状态?',{
            title:'提示',
            btn:['通过审核','审核失败'],
            btn1:function(index, layero){
                $.ajax({
                    type:"post",
                    url:'changeStatusOrder',
                    data:{
                        "id":id,'state':1
                    },
                    dataType:'json',
                    success:function(json,statusText){
                        if(json.errorCode == 0){
                            if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true,currentPage,pageSize);
                        }else{
                            responseTip(json.errorCode,json.errorInfo,1500);
                        }
                    }
                })
                layer.closeAll();

            },
            btn2:function(index){
                layer.closeAll();
                layer.prompt({title: '原因', formType: 2}, function(reason, index){
                    if(reason == ''){
                        return false;
                    }
                    $.ajax({
                        type:"post",
                        url:'changeStatusOrder',
                        data:{
                            "id":id,'state':2,'reason':reason
                        },
                        dataType:'json',
                        success:function(json,statusText){
                            layer.closeAll();
                            if(json.errorCode == 0){
                                if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                                    currentPage = currentPage - 1;
                                }
                                render(true,currentPage,pageSize);
                            }else{
                                responseTip(json.errorCode,json.errorInfo,1500);
                            }
                        }
                    })

                });
            },
        })
    }
    
    init();
});